package com.test.ddaidai.main_account_approve;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.test.ddaidai_android.activity.R;

public class OpenAccountCityFragmnent extends Fragment implements OnClickListener {

	private EditText enviroment_merchankey;
	private ImageButton merchankey_button;
	private PopupWindow popView;
	private MyAdapter dropDownAdapter;
	private String [] merchanids = {"--请选择开户城市--"};
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		
		View v = inflater.inflate(R.layout.fragmnent_openaccount_city, null);
		enviroment_merchankey = (EditText) v.findViewById(R.id.enviroment_merchankey);
		enviroment_merchankey.setText(merchanids[0]);
		merchankey_button = (ImageButton) v.findViewById(R.id.merchankey_button);
		merchankey_button.setOnClickListener(this);
		return v; 
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.merchankey_button:
			if (popView != null) {
				if (!popView.isShowing()) {
					popView.showAsDropDown(enviroment_merchankey);
				} else {
					popView.dismiss();
				}
			} else {
				if (merchanids.length > 0) {
					initPopView(merchanids);
					if (!popView.isShowing()) {
						popView.showAsDropDown(enviroment_merchankey);
					} else {
						popView.dismiss();
					}
				} else {
					Toast.makeText(this.getActivity(),"无记录", Toast.LENGTH_LONG).show();
				}
			}
			break;
		}
	}
	
	private void initPopView(String[] merchans) {
		List<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
		for (int i = 0; i < merchans.length; i++) {
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("name", merchans[i]);
			map.put("drawable", R.drawable.xicon);
			list.add(map);
		}
		dropDownAdapter = new MyAdapter(this.getActivity(), list, R.layout.item_popu_dropdown,
				new String[] { "name"}, new int[] { R.id.textview});
		
		ListView listView = new ListView(this.getActivity());
		listView.setDividerHeight(0);
		listView.setAdapter(dropDownAdapter);

		popView = new PopupWindow(listView, enviroment_merchankey.getWidth(),
				ViewGroup.LayoutParams.WRAP_CONTENT, true);
		popView.setFocusable(true);
		popView.setOutsideTouchable(true);
		popView.setBackgroundDrawable(getResources().getDrawable(R.drawable.white));
		// popView.showAsDropDown(mUserName);
	}

	class MyAdapter extends SimpleAdapter {

		private List<HashMap<String, Object>> data;

		public MyAdapter(Context context, List<HashMap<String, Object>> data,
				int resource, String[] from, int[] to) {
			super(context, data, resource, from, to);
			this.data = data;
		}

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(final int position, View convertView,
				ViewGroup parent) {
			System.out.println(position);
			ViewHolder holder;
			if (convertView == null) {
				holder = new ViewHolder();
				convertView = LayoutInflater.from(parent.getContext()).inflate(
						R.layout.item_popu_dropdown, null);
				holder.tv = (TextView) convertView.findViewById(R.id.textview);
				convertView.setTag(holder);
			} else {
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tv.setText(data.get(position).get("name").toString());
			holder.tv.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					enviroment_merchankey.setText(merchanids[position]);
					enviroment_merchankey.setSelection(enviroment_merchankey.getText().length());
					popView.dismiss();
				}
			});
			return convertView;
		}
	}
		
		class ViewHolder {
			private TextView tv;
		}
	
}
